package com.xw.kotlinlib.network

import com.google.gson.JsonSyntaxException
import retrofit2.HttpException
import java.io.IOException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException


/**
 * @param errorCode 异常的错误码
 * @param errorMsg 错误信息
 * @param originJson 原始的json数据
 */
class DataException(
    val errorCode: Int?,
    val errorMsg: String?,
    val originJson: String? = null
) : IOException(errorMsg)

/**
 * 获取友好的错误提示
 */
fun Throwable.getMsg(): String {
    return when (this) {
        is HttpException -> "网络异常:${code()}"
        is SocketTimeoutException -> "连接超时"
        is ConnectException -> "连接异常"
        is JsonSyntaxException, is UnsupportedOperationException -> "解析数据异常"
        is UnknownHostException -> "无法连接网络"
        is DataException -> errorMsg ?: "未知错误"
        else -> message ?: "未知错误"
    }
}